package com.atguigu.gulimall.seckill.interceptor;

import com.atguigu.common.constant.AuthServerConstant;
import com.atguigu.common.vo.MemberRespVo;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @Author baiWT
 * @Description
 * @Date 2022/4/8 14:37
 **/
@Component
public class LoginInterceptor implements HandlerInterceptor {

    public static ThreadLocal<MemberRespVo> loginUser = new ThreadLocal<>();

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        HttpSession session = request.getSession();
        MemberRespVo respVo = (MemberRespVo) session.getAttribute(AuthServerConstant.LOGIN_USER);
        if (respVo == null) {
            // 用户未登录
            response.sendRedirect("http://auth.gulimall.com/login.html");
            return false;
        } else {
            // 已登录，用threadLocal共享数据
            loginUser.set(respVo);
            return true;
        }
    }
}
